'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 扫荡
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var Code = require('../../../consts/code');
var moduleType = require('../../../consts/moduleType');
var operationType = require('../../../consts/operationType');
var copyConf = require('../../../config/copyConf');
var copyRewardService = require('../../../services/copyRewardService');
var roleService = require('../../../services/roleService');
var rewardService = require('../../../services/rewardService');
var PlayerManager = require('../../../libs/playerManager');
var taskService = require('../../../services/taskService');
var blackMarketConf = require('../../../config/blackMarketConf.js');

module.exports = function (app) {
    return new Handler(app);
};

var Handler = function (app) {
    this.app = app;
};

/**
 * 扫荡
 * @param  {[type]}   msg     [description]
 * @param  {[type]}   session [description]
 * @param  {Function} next    [description]
 * @return {[type]}           [description]
 */
Handler.prototype.sweep = function (msg, session, next) {
    var role_id = session.uid;
    var copy_id = msg.copy_id;
    var num = msg.num;
    if (!copy_id || !num) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    var player_manager = new PlayerManager(role_id);
    var sweep_list = [];
    async.waterfall(
        [

            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['role', 'copy', 'bag', 'build', 'soldier', 'skill', 'task', 'daily_task', 'achieve', 'tech', 'sevenDayActivity', 'accrued'],
                    cb);
            },
            // jshint maxstatements:50
            function (cb) {
                var role_model = player_manager.role;
                var copy_model = player_manager.copy;

                var copy_item = copy_model.getCopy(copy_id);
                if (!copy_item || copy_item.getStar() < 3) {
                    return next(null, {
                        code: Code.COPY_STAR_NEED_THREE
                    });
                }

                // 获取配置
                var copy_conf = copyConf.getCopyConf(copy_id);
                if (!copy_conf) {
                    return next(null, {
                        code: Code.COPY_NOT_EXIST
                    });
                }

                // 修改扫荡次数的判断
                var max_sweep_num = Math.floor(role_model.getFood() / copy_conf.food_cost);
                if (max_sweep_num <= 0) {
                    return next(null, {
                        code: Code.COPY_SWEEP_NUM_REACH_MAX
                    });
                }

                if (num > max_sweep_num) {
                    num = max_sweep_num;
                }

                // 扣除体力
                if (!role_model.subEnergy(copy_conf.sp_cost * num)) {
                    return next(null, {
                        code: Code.USER_SP_NOT_ENOUGH
                    });
                }

                for (var i = 0; i < num; i++) {
                    var add_exp = copy_conf.pass_exp_reward;
                    var add_gold = copy_conf.pass_gold_reward;
                    var reward_list = [];

                    // 是否有双倍卡次数true+英雄经验
                    // if (copy_model.getDoubleCopyExpNum() >= 1) {
                    //     copy_model.subDoubleCopyExpNum();
                    //     roleService.addExp(player_manager, add_exp * 2);
                    // } else {
                    //     roleService.addExp(player_manager, add_exp);
                    // }
                    roleService.addExp(player_manager, add_exp);

                    // 添加金币
                    roleService.addGoldLimit(player_manager, add_gold);

                    // 添加奖励
                    reward_list = copyRewardService.getReward(role_model.getJob(), copy_id);
                    rewardService.addRewardListToRole(player_manager, reward_list);

                    sweep_list.push({
                        add_exp: add_exp,
                        add_gold: add_gold,
                        reward_list: reward_list
                    });
                }

                // 增加副本扫荡统计
                taskService.addSecretsweep(player_manager);
                taskService.addPassCopyNum(player_manager);
                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: {
                    sweep_list: sweep_list
                }
            });
        }
    );
};
